<template>
  <el-card class="userAvatar-container">
    <!-- 面板屑导航 -->
    <el-breadcrumb separator="/" style="margin-bottom: 40px">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>个人中心</el-breadcrumb-item>
      <el-breadcrumb-item>更换头像</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 用来展示头像的盒子 -->
    <div class="avatar-box">
      <img :src="imgURL" alt="" />
    </div>
    <div class="btns">
      <!-- 文件选择框 -->
      <input type="file" hidden ref="fileRef" @change="onFileChange" />
      <el-button type="primary" icon="el-icon-plus" @click="chooseImage"
        >选择图片</el-button
      >
      <el-button
        type="success"
        icon="el-icon-upload"
        :disabled="isDisabled"
        @click="uploadAvatar"
        >上传头像</el-button
      >
    </div>
  </el-card>
</template>

<script>
// 导入默认图片
import img from '@/assets/images/avatar.jpg'
import { mapState, mapActions } from 'vuex'
import { updateUserAvatar } from '@/api/user'

export default {
  name: 'ChangeAvatar',
  data () {
    return {
      // 头像的图片
      imgURL: img,
      // 上传头像按钮禁用状态
      isDisabled: true
    }
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  created () {
    // console.log(this.userInfo)
    this.getUserInfo() // vuex的store的actions请求

    // userInfo=>{} => 有值 => 有值时,拿值 =>
    // 数据变化时 做事情 =>
    // =>

    // this.imgURL = this.userInfo.user_pic
  },
  watch: {
    // 只要可以this.xxx xxx就可以被watch侦听
    // data
    // computed
    // props
    // vuex的state => computed
    // this.$route.path
    // data/computed/props/ =>  userInfo
    // 1.0 简单数据
    // 被监听的数据名(a,b){}
    // 2.0 复杂数据
    userInfo: {
      deep: true,
      handler (v) {
        // console.log(v) // userInfo最新值
        this.imgURL = v.user_pic
      }
    }
  },
  methods: {
    ...mapActions('user', ['getUserInfo']),
    // 选择图片按钮
    chooseImage () {
      this.$refs.fileRef.click()
    },
    // 文件改变
    // TODO => 获取文件 + 文件转换
    onFileChange (e) {
      // console.log(e)// file
      // console.log(e.target.files[0])
      const file = e.target.files[0]
      if (file) {
        // file => base64
        const fr = new FileReader()
        // console.log(fr)
        // 下面的代码目前无结果 => 在fr的load事件里面可以获取到 => fr.result
        fr.readAsDataURL(file)
        fr.addEventListener('load', () => {
          // console.log(fr.result)
          const base64Str = fr.result
          // 预览
          this.imgURL = base64Str
          this.isDisabled = false
        })
      }
    },
    // 上传头像按钮
    async uploadAvatar () {
      const {
        data: { code, message }
      } = await updateUserAvatar(this.imgURL)
      if (code !== 0) return this.$message.error(message)
      this.$message.success(message)
      this.getUserInfo()
    }
  }
}
</script>

<style lang="less" scoped>
.userAvatar-container {
  padding: 0 0 20px 20px;

  .avatar-box {
    img {
      width: 350px;
      height: 350px;
      object-fit: cover;
    }
  }

  .btns {
    padding-top: 10px;
  }
}
</style>
